from .base_parser import BaseParser
import struct

class ModbusParser(BaseParser):
    def parse(self, raw_data: bytes) -> Dict[str, Any]:
        if len(raw_data) != 8:
            raise ValueError(f"Modbus数据长度错误，预期8字节，实际{len(raw_data)}字节")
        temperature = struct.unpack('!f', raw_data[:4])[0]
        humidity = struct.unpack('!f', raw_data[4:])[0]
        return {
            "temperature": round(temperature, 2),
            "humidity": round(humidity, 2),
            "protocol": "Modbus"
        }
    
    def get_metadata(self) -> Dict[str, str]:
        return {
            "format_name": "modbus_sensor_v1",
            "protocol": "Modbus",
            "version": "1.0",
            "description": "支持Modbus协议温湿度传感器数据解析"
        }